package com.imu.purse.modules.base.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.imu.core.base.BaseService;
import com.imu.datasources.DataSourceNames;
import com.imu.datasources.annotation.DataSource;
import com.imu.purse.modules.base.dao.UserLoginMapper;
import com.imu.purse.modules.base.entity.UserLoginEntity;
import com.imu.purse.modules.base.service.UserLoginService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 用户登录记录
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2017-11-08 10:31:21
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class UserLoginServiceImpl extends BaseService<UserLoginEntity> implements UserLoginService 
{
	@Override
    @Transactional
    public int save(final UserLoginEntity userLogin)
	{
		userLogin.setTimeCreate(new Date());
    	return super.insert(userLogin);
	}
	
	@Override
	@Transactional
    public int update(final UserLoginEntity userLogin) 
    {
    	return mapper.updateByPrimaryKeySelective(userLogin);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final UserLoginEntity userLogin)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(userLogin)))
    	{
        	len = this.insert(userLogin);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public UserLoginEntity findById(final Long sulId)
    {
    	if(null == sulId) return null;
    	return mapper.selectByPrimaryKey(sulId);
    }
    
    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public List<UserLoginEntity> queryList(final UserLoginEntity userLogin)
    {
    	if (userLogin.getPage() != null && userLogin.getLimit() != null)
    	{
            PageHelper.startPage(userLogin.getPage(), userLogin.getLimit());
        }
    	
    	return mapper.findByPage(userLogin);
    }
    
	@Autowired
	private UserLoginMapper mapper;
}
